<?php
require_once('inc/init.php');
require_once('inc/pager.php');
require_once getCacheFilePath('forums.php');
require_once getCacheFilePath('ads.php');

require_once('inc/editor/ubb.php');
//阅读权限
if(getPopedom(1)==0){
	printMsg('popedom_1');
}

$tid=numFilter($_GET['tid']);
$_SYS['viewurl']=genUrl("view.php?tid={$tid}");
$curPage = numFilter($_GET["page"]);
$curPage=$curPage==0?1:$curPage;
$topic=$db->row_select_one("topics,posts","topics.id={$tid} and topics.id=posts.tid and posts.istopic=1","*,posts.id as postid","topics.id");
if(empty($topic) || ($topic['isrecycle']==1 && !isForumAdmin($fid))){
	printMsg('view_topicnoexist');
}
if($topic['verifystate']>0 && !isForumAdmin($fid)){
	printMsg('view_topicnoverify');
}
$fid=$topic['fid'];
$forum = $cache_forums[$fid];
if(empty($forum)){
	printMsg('system_noforum');
}

if(!empty($forum['forumusers']) && !in_array($lg['username'],explode("\n",$forum['forumusers'])) && !isForumAdmin($fid)){
	printMsg('forum_forumusers');
}

if(!empty($forum['forumgroups']) && !in_array($lg['groupid'],explode(",",$forum['forumgroups'])) && !isForumAdmin($fid)){
	printMsg('forum_forumgroups');
}

if(!empty($forum['forumpass']) && getCookies("forumpass_{$fid}")!=$forum['forumpass'] && !isForumAdmin($fid)){
	printMsg('forum_forumpass');
}

$topic['title']=htmlFilter($topic['title']);
$topic['posttime']=getDateStr($topic['posttime']);
$headtitle=$topic['title']." - ".strip_tags($forum['forumname']);
$headkeywords=(empty($forum['metakeywords'])?"":$forum['metakeywords'].",");
$headdesc=htmlFilter(str_replace(array("\r", "\n"), array('', ''), cutStr($ubb->removeubb($topic['content']),200)));
$pager = new Pager();
$pager->init($cache_settings['postspernum'],$curPage,genUrl("view.php?tid={$tid}&page={page}"));
$posts=$pager->queryRows($db,"topics,users,posts","topics.id={$tid} and posts.userid=users.id and topics.id=posts.tid and posts.verifystate=0", "*,topics.id as topicid,users.id as userid","posts.istopic desc, posts.id");
$topic['hits']=$newobj["hits"]=$posts[0]['hits']+1;
$newobj["replynum"] = $pager->recordNum-1;
$db->row_update("topics",$newobj,"id={$tid}");
$replyaction="post.php?action=save&tid={$tid}&cmd=reply";

$adcode=getAd($fid);

$adminstr="<span class=\"adminstr\"><a href=\"javascript:postManager(1,{$topic['tid']},{$topic['postid']})\">删除</a> | <a href=\"javascript:postManager(2,{$topic['tid']},{$topic['postid']})\">移动</a> | <a href=\"javascript:postManager(3,{$topic['tid']},{$topic['postid']})\">屏蔽</a>| <a href=\"javascript:postManager(4,{$topic['tid']},{$topic['postid']})\">锁定</a> | <a href=\"javascript:postManager(5,{$topic['tid']},{$topic['postid']})\">置顶</a> | <a href=\"javascript:postManager(6,{$topic['tid']},{$topic['postid']})\">总置顶</a> | <a href=\"javascript:postManager(7,{$topic['tid']},{$topic['postid']})\">精华</a> | <a href=\"javascript:postManager(9,{$topic['tid']},{$topic['postid']})\">提升</a> | <a href=\"javascript:postManager(10,{$topic['tid']},{$topic['postid']})\">鉴定</a> | <a href=\"javascript:postManager(11,{$topic['tid']},{$topic['postid']})\">高亮</a></span>";
$iconstr='';
if($topic['stamp']>0){		//是否被鉴定过
	$stampstr="<div class='view_stamp'><img src='images/stamp/{$topic['stamp']}.gif' border=0 /></div>";
}
if($topic['islock']==1){			//是否被锁定
	$iconstr.="<img src='images/topic/lock.gif' border='0' title='锁定帖子' /> ";
}
if($topic['isgood']==1){			//是否精华
	$iconstr.="<img src='images/topic/good.gif' border='0' title='精华帖子' /> ";
}
if($topic['istop']==1){			//是否置顶
	$iconstr.="<img src='images/topic/top.gif' border='0' title='置顶帖子' /> ";
}
if($topic['isalltop']==1){			//是否总置顶
	$iconstr.="<img src='images/topic/alltop.gif' border='0' title='总置顶帖子' /> ";
}

$p=0;
$hadsolution=false;
$postarr=array();
foreach($posts as $key=>$post){
	$p++;
	$obj=array();
	$obj['adminstr']=isForumAdmin($fid)?" | <a href=\"javascript:postManager(1,".($post['istopic']==1?$post['tid']:0).",{$post['id']})\">删除</a> | <a href=\"javascript:postManager(3,0,{$post['id']})\">屏蔽</a>":"";
	$obj['posttime']=getDateStr($post['posttime']);
	$obj['signuptime']=getDateStr($post['signuptime'],'dateonly');
	$obj['editstr']= "<a href=\"post.php?action=editreply&postid={$post['id']}\">编辑</a> ";
	$obj['lockstr']='';
	$obj['goodstr']='';
	$obj['memberstr']='';
	$obj['votecontent']='';
	$obj['rewardcontent']='';
	$obj['beststr']='';
	$obj['userurl']=genUrl("user.php?uid={$post['userid']}");
	$obj['writemsg']=isLogin()?"<a href=\"javascript:void(0);\" onclick=\"sendUserMsg('{$post['username']}')\" class=\"writemsg\" title=\"给{$post['username']}发送站内短信\"></a>":"";
	
	if(!$hadsolution){
		$obj['beststr']="<a href=\"javascript:setBestAnswer({$tid},{$post['id']})\" class=\"best\">选为最佳答案</a>";
		if($topic['topictype']!=2 || $lg['userid']!=$topic['userid']){
			$obj['beststr']='';
		}
	}else{
		$obj['beststr']='';
	}
	if($post['istopic']==1){
		$obj['editstr'] = "<a href=\"post.php?action=edittopic&tid={$post['tid']}\">编辑</a> ";
		if($topic['islock']==1 && !isForumAdmin($fid)){
			$obj['editstr']='';
		}

		$obj['beststr']='';
		if($topic['topictype']==1){
			$obj['votecontent'].="<form name='voteform' id='voteform' onsubmit='return false;'><div class='view_vote'>";
				$allvotenum=$db->row_select_one("voteitems", "tid={$tid}","SUM(votenum) as allvotenum");
				$allvotenum=$allvotenum['allvotenum'];
				$allvotenum=$allvotenum==0?1:$allvotenum;
				
				$voterow=$db->row_select_one("votes", "tid={$tid}");
				$itemrows=$db->row_select("voteitems", "tid={$tid}", 0, "*","id");
				$votestarttime=getDateStr($voterow['starttime'], 'dateonly', false);
				$votestoptime=getDateStr($voterow['stoptime'], 'dateonly', false);
				
				$voteindex=1;
				$hasvote=false;
				$disabledstr='';
				$disabledtips='';
				if($voterow['starttime']>$_SYS['time'] || $voterow['stoptime']<$_SYS['time'] ){
					$disabledstr=" disabled='true' ";
					$disabledtips="<div style='margin-left:35px; margin-top:10px;'> 该投票已经过期，不能进行投票。</div>";
				}
				elseif(!isLogin()){
					$disabledstr=" disabled='true' ";
					$disabledtips="<div style='margin-left:35px; margin-top:10px;'> 您还没有登录，不能参与投票。</div>";
				}else{
					foreach($itemrows as $row){
						if(stristr(",{$row['voteuids']},", ",{$lg['userid']},")){
							$hasvote=true;
							$disabledstr=" disabled='true' ";
							$disabledtips ="<div style='margin-left:35px; margin-top:10px;'> 您已经投过票，谢谢您的参与。</div>";
							break;
						}
					}
				}
								
				
				if($voterow['maxvotes']>1){
					$inputtype='checkbox';
					$obj['votecontent'].="<div style='line-height:200%;'><b>多选投票</b>, 最多可以选 {$voterow['maxvotes']} 项, 共有 {$voterow['voteusernum']} 人参与投票 ".(($voterow['isopenvote']==1 && !($voterow['userid']!=$lg['userid'] && $voterow['viewresult']==1&&$hasvote==false))?"<a class=\"commonlink\" href=\"javascript:viewVoter({$tid});\">点击查看投票参与者</a>":"")."<br />投票时间: {$votestarttime} ~ {$votestoptime}</div>";
				}else{
					$inputtype='radio';
					$obj['votecontent'].="<div><b>单选投票</b>, 共有 {$voterow['voteusernum']} 人参与投票, 投票时间:{$votestarttime} ~ {$votestoptime}</div>";
				}

				foreach($itemrows as $item){
					if( $voterow['viewresult']==1&&$hasvote==false ){
						$item['votenum']=0;
						$votev=number_format($item['votenum']/$allvotenum*100,2);
						$width=0;
					}else{
						$votev=number_format($item['votenum']/$allvotenum*100,2);
						$width=400*$votev/100;
					}

					$obj['votecontent'].="<div style='height:35px; line-height:35px;'><input type='{$inputtype}' class='{$inputtype}_css' value='{$item['id']}' name='voteitemid[]' {$disabledstr} />{$voteindex}. {$item['title']}</div>";
					$obj['votecontent'].="<div style='padding-left:35px;'><div style='overflow:hidden; width:400px; height:12px; background:#dedede; display:inline-block;'><div style='width:{$width}px; height:12px; background:#57AE00;'></div></div> {$votev}% ({$item['votenum']})</div>";
					$voteindex++;
				}
			if(empty($disabledstr)){
				$obj['votecontent'].="<div style='margin-left:35px; margin-top:10px;'><input type='button' class='button_css' value='投票' onclick='postVote({$tid})' /> ".($voterow['isopenvote']==1?"(此为公开投票，其他人可看到您的投票项目)":"(此为非公开投票)")." </div>";
			}else{
				$obj['votecontent'].=$disabledtips;
			}
			$obj['votecontent'].="</div></form>";
		}elseif($topic['topictype']==2){
			$rewardrow=$db->row_select_one("reward","tid={$tid}");
			if($rewardrow['bestid']>0){
				$postrow=$db->row_select_one("posts","id={$rewardrow['bestid']}");
				$postrow['content']=$ubb->ubb2html($postrow['content']);
				$postrow['posttime']= getDateStr($postrow['posttime']);
				$answercontent="<div class='view_best'>";
				$answercontent.="<table><tr><td class='lefttitle'>最佳答案</td><td class='righttitle'><a href='".genUrl("user.php?uid={$postrow['userid']}")."' target='_blank'>{$postrow['username']}</a> 发表于 {$postrow['posttime']}</td></tr><tr><td class='left'><a href='".genUrl("user.php?uid={$postrow['userid']}")."' target='_blank'><img src='userpic.php?uid={$postrow['userid']}&type=small' border='0' align='absmiddle' /></a></td><td class='right'>{$postrow['content']}</td></tr></table>";
				$answercontent.="</div>";	
				$hadsolution=true;
			}			
			$obj['rewardcontent']="<div class='view_reward'>";
			$obj['rewardcontent'].="<span class='rewadtitle'>悬赏:</span> <span class='money'>{$cache_credits[$rewardrow['rewardcredits']]['creditsname']} {$rewardrow['reward']}</span> ".($hadsolution?"<span class=\"resolved\">该问题已经解决</span>":"<span class=\"goanswer\"><a href=\"post.php?action=reply&tid={$tid}\">[我来回答]</a></span>");
			$obj['rewardcontent'].="</div>".$answercontent;
		}
	}
	$posts[$key]['title']=htmlFilter($post['title']);	
	$posts[$key]['username']=htmlFilter($post['username']);	
	if($post['iscover']==1){
		$posts[$key]['content']="<span class='iscover'>帖子内容已经被屏蔽</span>";
		$obj['votecontent']='';
	}else{
		$posts[$key]['content']=$ubb->ubb2html($post['content']);
	}
	if($post['edittime']>0){
		$posts[$key]['edittips']="<div class='edittips'>该帖子于 ".getDateStr($post['edittime'])." 编辑过</div>";
	}
	$posts[$key]['ip']=isForumAdmin($fid)?"发帖IP <span class=\"ip\"><a href='".str_replace('{ip}',$post['ip'], $cache_settings['iplink'])."' target='_blank'>{$post['ip']}</a></span>":'';
	$obj['memberstr'].=	"<dl class='view_a3'>".
							"<dt>帖子</dt><dd>{$post['postsnum']}&nbsp;</dd>";
	foreach($cache_credits as $credits){
		$obj['memberstr'].="<dt>{$credits['creditsname']}</dt><dd>{$post[$credits['creditscode']]}&nbsp;</dd>";
	}
	if(!empty($post['usertitle'])){
		$obj['memberstr'].=	"<dt>头衔</dt><dd><span style='color:{$cache_groups[$post[groupid]][color]}'>".htmlFilter($post['usertitle'])."</span>&nbsp;</dd>";
	}
	$obj['memberstr'].=	"<dt>注册</dt><dd>{$obj['signuptime']}&nbsp;</dd>".
							"</dl>";
	$obj['postad']=ad2html($adcode[4], 'random');
	if($post['userid']!=$lg['userid'] && !isForumAdmin($fid)){
		$obj['editstr']='';
	}
	$obj['starimg']=getStar($cache_groups[$post[groupid]][starnum]);
	$obj['signature']=empty($post['signature'])?"":"<div class=signature>".$ubb->ubb2html($post['signature'])."</div>";
	
	$postarr[$p]=$obj;
	$floor=($curPage-1)*10+$p;
}



require_once('header.php');
require_once getTemplatePath('view.htm');
footer();
?>